package cn.kgc.vuedemoebook2.controller;

import cn.kgc.vuedemoebook2.pojo.Entry;
import cn.kgc.vuedemoebook2.pojo.ResultEntity;
import cn.kgc.vuedemoebook2.service.EntryServcie;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author niejianhang
 * @create 2020-05-19 14:37
 */
@RestController
public class EntryController {


    @Resource
    EntryServcie entryServcie;


    //根据分类id查询
    @RequestMapping("/selBycategoryId")
    public ResultEntity<PageInfo<Entry>> selBycategoryId(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                                                         @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,

                                                         @RequestParam(value = "categoryId",defaultValue = "0",required = false)Integer categoryId){
        PageInfo<Entry> pageInfo = entryServcie.selAllBycategoryId(pageNum, pageSize, categoryId);


        return ResultEntity.successWithoutData(pageInfo);
    }

    //增加或保存 无id的实体类为增加 有id的实体类为修改
    @RequestMapping("/saveEntry")
    public ResultEntity<Integer> saveEntry(Entry entry){
        Integer i;
        if(entry.getId()==null){
            i=entryServcie.add(entry);
        }else {
            i=entryServcie.update(entry);
        }


        return ResultEntity.successWithoutData(i);
    }
    //根据id查询书
    @RequestMapping("/selById")
    public ResultEntity<Entry> selById(Integer id){
        Entry entry = entryServcie.selById(id);

        return ResultEntity.successWithoutData(entry);
    }

    //根据id 删除
    @RequestMapping("/delById")
    public ResultEntity<Integer> delById(Integer id){
        int del = entryServcie.del(id);

        return ResultEntity.successWithoutData(del);
    }



}
